package com.flashmoment.video.controller;

import java.util.*;

import cn.hutool.Hutool;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.db.sql.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.flashmoment.video.entity.UserCollectEntity;
import com.flashmoment.video.service.UserCollectService;
import com.flashmoment.video.utils.Constant;
import com.flashmoment.video.utils.PageUtils;
import com.flashmoment.video.utils.R;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.flashmoment.video.entity.VideoEntity;
import com.flashmoment.video.service.VideoService;




/**
 * 视频表
 *
 * @author xulu
 * @email 1435533194@qq.com
 * @date 2023-10-24 16:35:33
 */
@RestController
@RequestMapping("video/video")
@CrossOrigin
@Log4j2
@Api(tags = "视频接口")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private UserCollectService collectService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){

        try {
            PageUtils  page = videoService.queryPage(params);
            return R.ok().put("page", page);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e);
            log.error(e.getMessage());
          return   R.error("获取视频失败");
        }
    }
    /**
     * 查询收藏
     */
    @PostMapping("/select/collect")
    public R SelectCollect(@PathVariable long id)
    {
        List<String> list=videoService.SelectCollectAll(id);
        return R.ok();
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		VideoEntity video = videoService.getById(id);

        return R.ok().put("video", video);
    }
    @RequestMapping("getLikeNum")
    public R getLikeNum(Integer id){
        long video_count = collectService.count(Wrappers.<UserCollectEntity>query().eq("video_id", id).eq("status","1"));
        return R.ok().put("LikeNum",video_count);
    }

    @RequestMapping("getCollectNum")
    public R getCollectNum(Integer id){
        long video_count = collectService.count(Wrappers.<UserCollectEntity>query().eq("video_id", id).eq("status","2"));
        return R.ok().put("collectNum",video_count);
    }
    @RequestMapping("getShareNum")
    public R getShareNum(Integer id){
        long video_count = collectService.count(Wrappers.<UserCollectEntity>query().eq("video_id", id).eq("status","3"));
        return R.ok().put("shareNum",video_count);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody VideoEntity video){
        try {
            video.setCreateTime(DateUtil.date());
            video.setUpdateTime(DateUtil.date());
            video.setStatus(1);
            boolean save = videoService.save(video);
            if (!save){
                return R.error(3001,"视频上传失败");
            }
            return R.ok().put("1000","上传成功");
        }catch (Exception e){
            return R.error(3002,"视频上传失败");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody VideoEntity video){
		videoService.updateById(video);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		videoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据前端搜索的关键字从es中查找数据,以分页形式展示,并实现高亮
     * @return
     */
    //创建一个高亮+搜索的方法
    @GetMapping("/findVideoData/{pageNumber}/{pageSize}/{keywords}")
    @ResponseBody
    public R findGoodsData(@PathVariable("pageNumber") int pageNumber,
                           @PathVariable("pageSize") int pageSize,
                           @PathVariable("keywords") String keywords){

        //返回方法结果
        List<Map<String, Object>> list = videoService.findVideoData(pageNumber, pageSize, keywords);
        if (list.isEmpty()){
            return Objects.requireNonNull(new R().put("code", Constant.FAILURE_CODE)).put("message",Constant.SEARCH_FAILURE);
        }
        return Objects.requireNonNull(new R().put("code", Constant.SUCCESS_CODE)).put("message",Constant.SEARCH_SUCCESS).put("list",list);

    }
    @GetMapping("/findVideoById/{id}")
    public R findVideoById(@PathVariable Integer id){
        List<VideoEntity> video = videoService.findVideoById(id);
        if (video.isEmpty()){
            return Objects.requireNonNull(new R().put("code", Constant.FAILURE_CODE)).put("message",Constant.SEARCH_FAILURE);
        }
        return Objects.requireNonNull(new R().put("code", Constant.SUCCESS_CODE)).put("message",Constant.SEARCH_SUCCESS).put("list",video);

    }

}
